package com.jeesite.modules.app.question.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.modules.app.mockexam.special.record.entity.AppMockexamSpecialRecord;
import com.jeesite.modules.app.mockexam.special.record.service.AppMockexamSpecialRecordService;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;
import com.jeesite.modules.zx.question.bank.entity.TquestionBank;
import com.jeesite.modules.zx.question.bank.service.TquestionBankService;

import cn.hutool.db.Entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/app/question/")
public class AppQuestionBankController {

    @Autowired
    private TquestionBankService tquestionBankService;
    @Autowired
    private AppMockexamSpecialRecordService appMockexamSpecialRecordService;
    @RequestMapping(value="findList")
    public void list(TquestionBank tquestionBank,HttpServletRequest request,HttpServletResponse response) {
    	String userId=request.getParameter("userId");
    	Integer knowledgePointType = tquestionBank.getKnowledgePointType();
    	JSONObject json=new JSONObject();
    	json.put("error", 0);
    	json.put("success", 0);
    	if(StringUtils.isNotBlank(userId)) {
    		//查询错误数
    		AppMockexamSpecialRecord record=new AppMockexamSpecialRecord();
    		record.setUserId(userId);
    		record.setPostId(knowledgePointType.toString());
    		record.setScore(new BigDecimal(0.0));
			long error = appMockexamSpecialRecordService.findCount(record);
			json.put("error", error);
			//正确数
			AppMockexamSpecialRecord recordSucc=new AppMockexamSpecialRecord();
			recordSucc.setUserId(userId);
			recordSucc.setPostId(knowledgePointType.toString());
			recordSucc.getSqlMap().getWhere().and("score", QueryType.GT, 0);
			long success = appMockexamSpecialRecordService.findCount(recordSucc);
			json.put("success", success);
    	}
        tquestionBank.getSqlMap().getOrder().setOrderBy("objType asc");
        List<TquestionBank> findList = tquestionBankService.findList(tquestionBank);
        JSONArray parseObject = JSONObject.parseArray(JsonMapper.toJson(findList));
        json.put("list", parseObject);
        AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
    }

    
    @RequestMapping(value="findListByPost")
    public void findListByPost(@RequestParam(value = "post",required = true) String post, HttpServletResponse response) {
        // 单选30，多选20，判断20
        List<TquestionBank> quetbankList = new ArrayList<>();
        TquestionBank single=new TquestionBank();
        single.setObjType(0);
        single.setPost(post);
        single.getSqlMap().getOrder().setOrderBy("RAND() LIMIT "+ 30);

        TquestionBank multiple=new TquestionBank();
        multiple.setObjType(1);
        multiple.setPost(post);
        multiple.getSqlMap().getOrder().setOrderBy("RAND() LIMIT "+ 20);

        TquestionBank judge=new TquestionBank();
        judge.setObjType(2);
        judge.setPost(post);
        judge.getSqlMap().getOrder().setOrderBy("RAND() LIMIT "+ 20);

        quetbankList.addAll(tquestionBankService.findList(single));
        quetbankList.addAll(tquestionBankService.findList(multiple));
        quetbankList.addAll(tquestionBankService.findList(single));

        JSONArray parseObject = JSONObject.parseArray(JsonMapper.toJson(quetbankList));
        AppController.renderSuccessObject(response, parseObject, MessageCode.SUCCESS_ACK);
    }
}
